package com.xh.cdp.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.xh.cdp.common.BaseController;
import com.xh.cdp.common.EasyUIPageModel;
import com.xh.cdp.sys.entity.SysUserEntity;
import com.xh.cdp.sys.service.SysUserService;

@Controller
@RequestMapping("/sys/user")
public class UserController extends BaseController {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 用户登录
	 * 
	 * @param loginCode
	 * @param loginPassword
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public String login(SysUserEntity user, HttpSession session,
			RedirectAttributesModelMap ram) {
		SysUserEntity sysUserEntity = sysUserService.login(user.getCode(),
				user.getPassword());
		if (sysUserEntity != null) {
			session.setAttribute("user", sysUserEntity);
			return "redirect:index";
		}
		ram.addFlashAttribute("msg", "fail");
		return "redirect:/sys/user/toHome";
	}

	/**
	 * 返回地址REST风格尚未实现
	 * 
	 * @param msg
	 * @return
	 */
	@RequestMapping("/toHome")
	public String toHome(@ModelAttribute("msg") String msg) {
		return "redirect:/";
	}

	@RequestMapping("/listUser")
	@ResponseBody
	public EasyUIPageModel<SysUserEntity> listUser(String sort, String order,
			int page, int rows) {
		return sysUserService.listUser(sort, order, page, rows);
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public String addUser(SysUserEntity user) {
		return sysUserService.addUser(user);
	}

	@RequestMapping("/showUserInfo")
	@ResponseBody
	public SysUserEntity showUserInfo(String id) {
		return sysUserService.showUserInfo(id);
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateUser(SysUserEntity user) {
		return sysUserService.updateUser(user);
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public String deleteUser(String ids) {
		Integer row = sysUserService.deleteUser(ids);
		if (row > 0) {
			return "success";
		}
		return "";
	}

	@RequestMapping("/listUserBySearch")
	@ResponseBody
	public EasyUIPageModel<SysUserEntity> listUserBySearch(String sort,
			String order, Integer page, Integer rows, String searchName,
			String roleId, Integer state) {
		return sysUserService.listUserBySearch(sort, order, page, rows,
				searchName, roleId, state);
	}

	@RequestMapping("/updateUserState")
	@ResponseBody
	public Integer updateUserState(SysUserEntity user) {
		return sysUserService.updateUserState(user);
	}

	@RequestMapping("/showUserInfoByLoginer")
	@ResponseBody
	public SysUserEntity showUserInfoByLoginer(HttpSession session) {

		SysUserEntity sysUserEntity = (SysUserEntity) session
				.getAttribute("user");
		System.out.println(sysUserEntity);
		return sysUserService.showUserInfo(sysUserEntity.getId());
	}

	@RequestMapping("/userLogout")
	public String userLogout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "redirect:/login.jsp";
	}

	@RequestMapping("/equalPassword")
	@ResponseBody
	public String equalPassword(String password, HttpSession session) {
		SysUserEntity sysUserEntity = (SysUserEntity) session
				.getAttribute("user");
		return sysUserService.equalPassword(password, sysUserEntity.getId());
	}

	@RequestMapping("/updatePassword")
	@ResponseBody
	public String updatePassword(String newPassword, HttpSession session) {
		SysUserEntity sysUserEntity = (SysUserEntity) session
				.getAttribute("user");
		sysUserService.updatePassword(newPassword, sysUserEntity.getId());
		return "";
	}

	/**
	 * 跳转至后台页面
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "sys/index";
	}

}
